
export default {
    tips: function (type, message) {
        Message({
            type: type,
            message: message || '服务器错误',
            center: true,
            offset: 100, //距离顶部的距离
            duration: 3000,
        });
    },
    storage: function () {
        return {
            get: function (name) {
                return JSON.parse(sessionStorage.getItem(name));
            },
            set: function (name, val) {
                sessionStorage.setItem(name, JSON.stringify(val));
            },
            remove: function (name) {
                sessionStorage.removeItem(name)
            }
        }
    }(),
    localStorage: function () {
        return {
            get: function (name) {
                return JSON.parse(localStorage.getItem(name)) || {};
            },
            set: function (name, val) {
                localStorage.setItem(name, JSON.stringify(val));
            },
            remove: function (name) {
                localStorage.removeItem(name)
            }
        }
    }(),

    setCookie: function (name, value) {
        let exp = new Date();
        exp.setTime(exp.getTime() + 3 * 24 * 60 * 60 * 1000); //3天过期
        document.cookie = name + "=" + encodeURIComponent(value) + ";expires=" + exp.toGMTString() + ";path=/";
        return true;
    },
    getCookie: function (name) {
        let arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
        if (arr = document.cookie.match(reg))
            return unescape(arr[2]);
        else
            return null;
    },
    clearCookie: function (name) {
        var exp = new Date();
        exp.setTime(exp.getTime() - 1);
        var cval = this.getCookie(name);
        if (cval != null)
            document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
    },
    getUrlParam: function (name) {
        let _s = window.location.search;
        if (!_s) return '';
        _s = _s.substr(1);
        let _arr = _s.split('&');
        let _t = null;
        for (let i = 0; i < _arr.length; i++) {
            _t = _arr[i].split('=');
            if (_t[0] == name) {
                return _t.length > 1 ? _t[1] : '';
            }
        }
        return '';
    },
    formatTime(num) {
        var date = new Date(num) //时间戳为10位需*1000，时间戳为13位的话不需乘1000
        var Y = date.getFullYear() + '-'
        var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
        var D = date.getDate() + ' '
        var h = date.getHours() + ':'
        var m = date.getMinutes() + ':'
        var s = date.getSeconds()
        // return Y+M+D;
        return Y + M + D + h + m + s
    },
    timeFormat(num) {
        var date = new Date(num.replace(/-/g,"/")) //时间戳为10位需*1000，时间戳为13位的话不需乘1000
        console.log(date);

        var Y = date.getFullYear() + '-'
        var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
        var D = date.getDate() + ' '
        var h = date.getHours() + ':'
        var m = date.getMinutes() + ':'
        var s = date.getSeconds()
        console.log(Y + M + D);
        return Y + M + D;
    },
    // 将base64转换为文件
    dataURLtoFile(dataurl, filename) {
        var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
            bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
        while (n--) {
            u8arr[n] = bstr.charCodeAt(n);
        }
        return new File([u8arr], filename, { type: mime });
    },
    // // 压缩图片
    compressImages(file, callback) {
        // 缩放图片需要的canvas
        var canvas = document.createElement('canvas');
        var context = canvas.getContext('2d');
        var reader = new FileReader(), imgFile = file;
        reader.readAsDataURL(imgFile);

        var fileName = imgFile.name.substring(0, imgFile.name.indexOf(".")) + '.jpg';

        var img = new Image();
        // 文件base64化，以便获知图片原始尺寸
        reader.onload = function (e) {
            img.src = e.target.result;
            // base64地址图片加载完毕后
            img.onload = function () {
                // 图片原始尺寸
                var originWidth = this.width;
                var originHeight = this.height;
                // 最大尺寸限制
                var maxWidth = 100,
                    maxHeight = 143;
                // 目标尺寸
                var targetWidth = originWidth,
                    targetHeight = originHeight;
                // 图片尺寸超过1280x720的限制
                if (originWidth > maxWidth || originHeight > maxHeight) {
                    if (originWidth / originHeight > maxWidth / maxHeight) {
                        // 更宽，按照宽度限定尺寸
                        targetWidth = maxWidth;
                        targetHeight = Math.round(maxWidth * (originHeight / originWidth));
                    } else {
                        targetHeight = maxHeight;
                        targetWidth = Math.round(maxHeight * (originWidth / originHeight));
                    }
                }
                // canvas对图片进行缩放
                canvas.width = targetWidth;
                canvas.height = targetHeight;
                // 清除画布
                context.clearRect(0, 0, targetWidth, targetHeight);
                // 图片压缩
                context.drawImage(img, 0, 0, targetWidth, targetHeight);
                var base64 = canvas.toDataURL('image/jpeg', 0.92);
                var newfile = this.dataURLtoFile(base64, fileName)
                // callback(base64)
                callback(newfile)
                // resolve(base64);
            };

        };

    },

    // 视频时长转换 00:00:00
    timeToFormat(times) {
        var result = '00:00:00';
        var hour, minute, second
        if (times > 0) {
            hour = Math.floor(times / 3600);
            if (hour < 10) {
                hour = "0" + hour;
            }
            minute = Math.floor((times - 3600 * hour) / 60);
            if (minute < 10) {
                minute = "0" + minute;
            }

            second = Math.floor((times - 3600 * hour - 60 * minute) % 60);
            if (second < 10) {
                second = "0" + second;
            }
            result = hour + ':' + minute + ':' + second;
        }
        return result;
    },

    // 转文件大小
    sizeFilter(limit) {
        var size = "";
        if (limit < 0.1 * 1024) {
            //如果小于0.1KB转化成B
            size = limit.toFixed(2) + "B";
        } else if (limit < 0.1 * 1024 * 1024) {
            //如果小于0.1MB转化成KB
            size = (limit / 1024).toFixed(2) + "KB";
        } else if (limit < 0.1 * 1024 * 1024 * 1024) {
            //如果小于0.1GB转化成MB
            size = (limit / (1024 * 1024)).toFixed(2) + "MB";
        } else {
            //其他转化成GB
            size = (limit / (1024 * 1024 * 1024)).toFixed(2) + "GB";
        }

        var sizestr = size + "";
        var len = sizestr.indexOf(".");
        var dec = sizestr.substr(len + 1, 2);
        if (dec == "00") {
            //当小数点后为00时 去掉小数部分
            return sizestr.substring(0, len) + sizestr.substr(len + 3, 2);
        }
        return sizestr;
    },

    // 给不同的文件加相应的图标
    addIcon(strRes) {
        var filetype = "";
        if (strRes == "doc" || strRes == "docx") {
            filetype = "1";
        } else if (strRes == "xls" || strRes == "xlsx") {
            filetype = "2";
        } else if (strRes == "ppt" || strRes == "pptx") {
            filetype = "3";
        } else if (strRes == "txt") {
            filetype = "4";
        } else if (strRes == "pdf") {
            filetype = "5";
        } else if (
            strRes == "mp3" ||
            strRes == "mp2" ||
            strRes == "m4r" ||
            strRes == "m4a" ||
            strRes == "ac3" ||
            strRes == "wma" ||
            strRes == "flac" ||
            strRes == "aac" ||
            strRes == "amr" ||
            strRes == "ogg" ||
            strRes == "ape" ||
            strRes == "midi" ||
            strRes == "aiff" ||
            strRes == "wave" ||
            strRes == "cd"
        ) {
            filetype = "6";
        } else if (
            strRes == "avi" ||
            strRes == "mp4" ||
            strRes == "rmvb" ||
            strRes == "rm" ||
            strRes == "flv" ||
            strRes == "3gp" ||
            strRes == "wmv" ||
            strRes == "asf" ||
            strRes == "mkv" ||
            strRes == "f4v" ||
            strRes == "mpg" ||
            strRes == "swf" ||
            strRes == "vob" ||
            strRes == "mov" ||
            strRes == "mpeg" ||
            strRes == "mpe" ||
            strRes == "m4v"
        ) {
            filetype = "7";
        } else if (strRes == "png" || strRes == "jpg" || strRes == "bmp") {
            filetype = "8";
        } else if (strRes == "rar" || strRes == "zip") {
            filetype = "9";
        } else {
            filetype = ""
        }
        return filetype

    }

}
